package com.zdsoft.finance.project.service.impl;

import com.zdsoft.finance.base.service.impl.BaseServiceImpl;
import com.zdsoft.finance.common.base.CustomRepository;
import com.zdsoft.finance.common.exception.BusinessException;
import com.zdsoft.finance.common.utils.ObjectProperUtil;
import com.zdsoft.finance.project.entity.Project;
import com.zdsoft.finance.project.repository.ProjectRepository;
import com.zdsoft.finance.project.service.ProjectService;
import com.zdsoft.framework.core.common.annotation.Log;
import com.zdsoft.framework.core.common.domain.BaseEntity;
import com.zdsoft.framework.core.common.util.ObjectHelper;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目service实现类
 * 
 * @author LiaoGuoWei
 * @create 2016-11-14 15:20
 **/
@Service("projectService")
public class ProjectServiceImpl extends BaseServiceImpl<Project, CustomRepository<Project, String>>
		implements ProjectService {

	@Autowired
	ProjectRepository projectRepository;

	@Log
	Logger logger;

	@Override
	@Transactional
	public Project saveProject(Project project) throws BusinessException {
		if (ObjectHelper.isNotEmpty(project)) {
			return this.projectRepository.saveEntity(project);
		} else {
			throw new BusinessException("10010004", "保存项目出错！传入参数为空");
		}
	}

	@Override
	@Transactional
	public Project updateProject(Project project) throws BusinessException {
		if (ObjectHelper.isNotEmpty(project)) {
			Project pro = this.projectRepository.findOne(project.getId());
			pro = (Project) ObjectProperUtil.compareAndValue(project, pro);
			return this.projectRepository.saveEntity(pro);
		} else {
			throw new BusinessException("10010004", "更新项目出错！传入参数为空");
		}
	}

	@Override
	public Project findByProjectCd(String projectCd) throws BusinessException {
		if (ObjectHelper.isNotEmpty(projectCd)) {
			return this.projectRepository.findByProjectCode(projectCd);
		} else {
			throw new BusinessException("10010004", "通过项目编号查找项目出错！传入参数为空");
		}
	}

	@Override
	public Project findByBusiCd(String busiCd) throws BusinessException {
		if (ObjectHelper.isNotEmpty(busiCd)) {
			return this.projectRepository.findByBusiCd(busiCd);
		} else {
			throw new BusinessException("10010004", "通过业务编号查找项目出错！传入参数为空");
		}
	}

	@Override
	public List<Project> findByConditions(Map<String, Object> conditions) throws BusinessException {
		if (ObjectHelper.isNotEmpty(conditions)) {
			Map<String, Object> qryCondition = new HashMap<>();
			StringBuffer hql = new StringBuffer("select p from Project p where p.isDeleted= :isDeleted ");
			qryCondition.put("isDeleted", !BaseEntity.DELETED);

			// 項目編號
			if (ObjectHelper.isNotEmpty(conditions.get("projectCd"))) {
				hql.append(" and p.projectCd = :projectCd ");
				qryCondition.put("projectCd", conditions.get("projectCd"));
			}
			// 項目名稱
			if (ObjectHelper.isNotEmpty(conditions.get("projectNm"))) {
				hql.append(" and p.projectNm like :projectNm ");
				qryCondition.put("projectNm", "%" + conditions.get("projectNm") + "%");
			}
			// 業務編號
			if (ObjectHelper.isNotEmpty(conditions.get("busiCd"))) {
				hql.append(" and p.busiCd like :busiCd ");
				qryCondition.put("busiCd", "%" + conditions.get("busiCd") + "%");
			}
			// 項目狀態
			if (ObjectHelper.isNotEmpty(conditions.get("status"))) {
				hql.append(" and p.status = :status ");
				qryCondition.put("status", conditions.get("status"));
			}
			// 所属业务部门/公司编号
			if (ObjectHelper.isNotEmpty(conditions.get("belongOrgCd"))) {
				hql.append(" and p.belongOrgCd = :belongOrgCd ");
				qryCondition.put("belongOrgCd", conditions.get("belongOrgCd"));
			}
			// 所属业务部门/公司名称
			if (ObjectHelper.isNotEmpty(conditions.get("belongOrgNm"))) {
				hql.append(" and p.belongOrgNm like :belongOrgNm ");
				qryCondition.put("belongOrgNm", "%" + conditions.get("belongOrgNm") + "%");
			}
			// 客戶編號
			if (ObjectHelper.isNotEmpty(conditions.get("clientCd"))) {
				hql.append(" and p.clientCd = :clientCd ");
				qryCondition.put("clientCd", conditions.get("clientCd"));
			}
			// 客戶名稱
			if (ObjectHelper.isNotEmpty(conditions.get("clientNm"))) {
				hql.append(" and p.clientNm like :clientNm ");
				qryCondition.put("clientNm", "%" + conditions.get("clientNm") + "%");
			}
			// 經辦部門編號
			if (ObjectHelper.isNotEmpty(conditions.get("handlingDepartmentCd"))) {
				hql.append(" and p.handlingDepartmentCd = :handlingDepartmentCd ");
				qryCondition.put("handlingDepartmentCd", conditions.get("handlingDepartmentCd"));
			}
			// 經辦部門名稱
			if (ObjectHelper.isNotEmpty(conditions.get("handlingDepartmentNm"))) {
				hql.append(" and p.handlingDepartmentNm like :handlingDepartmentNm ");
				qryCondition.put("handlingDepartmentNm", "%" + conditions.get("handlingDepartmentNm") + "%");
			}
			hql.append(" order by p.createTime desc ");
			return this.projectRepository.findByHql(hql.toString(), qryCondition);
		} else {
			throw new BusinessException("10010004", "通过条件查找项目出错！传入参数为空");
		}
	}

	  @Override
	  public Project findById(String id) throws BusinessException {
		    if(ObjectHelper.isNotEmpty(id)){
				 Project project=this.projectRepository.findOne(id);
				 if(ObjectHelper.isNotEmpty(project)){
					   return project;
				 }else{
					   throw new BusinessException("10010002","根据相应参数未找到相应的数据");
				 }
		    }
		    else {
				throw  new BusinessException("10010004","传入参数为空");
		    }
	  }
}
